package agv.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import agv.entity.*;
import agv.httpclient.HttpClientUtil;
import com.alibaba.fastjson.JSONObject;

public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String uri = request.getRequestURI();
		uri = uri.substring(uri.indexOf("MyProject/") + 10, uri.lastIndexOf("."));
		if (uri.equals("show")) {
			try {
				show(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// ���÷���
	public void show(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String reqCode = CodeGenerateUtil.generate();
		String taskTyp = request.getParameter("id"); // ����id��ѯ
		List<String> userCallCodePath = new ArrayList<String>();
		userCallCodePath.add("A");
		userCallCodePath.add("B");
		TaskDemo td = new TaskDemo(reqCode,taskTyp,userCallCodePath);//ƴװ����
		String resultMsg = testDemoPost(td);// �ͻ��˴��ݹ�ȥ�Ĳ���Ϊ����
		request.setAttribute("userList", userCallCodePath); // ����ҳ��
		request.getRequestDispatcher("record.jsp").forward(request, response);
	}
	/**
	 * @author wufei6 2017��3��30��17:20:36
	 * @param
	 * */
	public String testDemoPost(TaskDemo td) throws Exception {
		String resultMsg = null;//获取服务端返回参数
		String url = "http://localhost:8090/rcs/services/rest/hikRpcService/genAgvSchedulingTask";
		String param = JSONObject.toJSONString(td);//将对象转为json格式
		resultMsg = HttpClientUtil.post(url, param);// post 形式调用webservice
		if (toString().isEmpty()) {
			return resultMsg;
		}
		System.out.println(resultMsg);
		return resultMsg;
	}
}